//表格初始化
$(document).ready(function() {
    $('#relation_table').DataTable( {
        // fixedHeader: true
    } );
} );
//查询方法
let optionJson;
function user_relation(){
    let request_parameters = {type:"get_all_user_relation_info"};
    $.ajax({
        type:"post",
        url:"http://127.0.0.1:9998/show/get_all_user_relation_info",
        data:request_parameters,
        async: false,
        success:function (all_user_relation_info) {
            console.log(all_user_relation_info);
             optionJson = all_user_relation_info;
            $('#relation_table').dataTable().fnDestroy();//还原初始化
            $('#relation_table').DataTable({
                // "serverSide":true,
                //  "paging":true,
                // "lengthMenu":[15,25,35,100,25],
                // "pagingType":"scrolling",
                // serverSide:true,
                // Processing:true,
                // deferRender:true,
                "bStateSave":true,//翻页时保存状态
                // iDisplayLength:10,
                // iDisplayStart:0,
                "ordering":false,//禁用全局排序
                "sort":"position",
                data:optionJson.data,
                //设置列数据
                columns:[
                    {"data": 'phoneNum1', "width":'13%',
                        "render":function (data,type,row,meta) {
                            return data = '<p style="text-align: center">'+data+'</p>'
                        }
                    },
                    {"data": 'phoneNum2', "width":'13%',
                        "render":function (data,type,row,meta) {
                            return data = '<p style="text-align: center">'+data+'</p>'
                        }
                        },
                    {"data": 'callNumber', "width":'7%',
                        "render":function (data,type,row,meta) {
                            return data = '<p style="text-align: center">'+data+'</p>'
                        }
                    },
                    {"data": 'messageNumber',"width":'7%',
                        "render":function (data,type,row,meta) {
                            return data = '<p style="text-align: center">'+data+'</p>'
                        }
                    },
                    {"data": 'contactTime', "width":'10%',
                        "render":function (data,type,row,meta) {
                            return data = '<p style="text-align: center">'+data+'</p>'
                        }
                    },
                    {"data": 'appellation', "width":'15%',
                        "render":function (data,type,row,meta) {
                            return data = '<p style="text-align: center">'+data+'</p>'
                        }
                    },
                    {"data": 'topicKeyword', "width":'15%',
                        "render":function (data,type,row,meta) {
                            return data = '<p style="text-align: center">'+data+'</p>'
                        }
                    },
                    {"data": 'relation', "width":'10%',
                        "render":function (data,type,row,meta) {
                            return data = '<p style="text-align: center">'+data+'</p>'
                        }
                    },
                    {"data": 'reStrength', "width":'10%',
                        "render":function (data,type,row,meta) {
                            return data = '<p style="text-align: center">'+data+'</p>'
                        }
                    }
                ],
                //设置排序
                "order":[[1,'asc']],
                //设置语言
                language: {
                    "sProcessing": "处理中...",
                    "sLengthMenu": "显示 _MENU_ 项搜索结果",
                    "sZeroRecords": "没有匹配结果",
                    "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 条数据",
                    "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
                    "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
                    "sInfoPostFix": "",
                    "sSearch": "搜索:",
                    "sUrl": "",
                    "sEmptyTable": "未搜索到数据",
                    "sLoadingRecords": "载入中...",
                    "sInfoThousands": ",",
                    "oPaginate": {
                        "sFirst": "首页",
                        "sPrevious": "上页",
                        "sNext": "下页",
                        "sLast": "末页"
                    },
                    "oAria": {
                        "sSortAscending": ": 以升序排列此列",
                        "sSortDescending": ": 以降序排列此列"
                    }
                },
            });
        }
    });
}
function user_relation_Excelexport() {
            //输出base64编码
            //要导出的json数据
            const jsonData =optionJson.data;
            //列标题，逗号隔开，每一个逗号就是隔开一个单元格
            let str = `号码1,号码2,通话条数,短信条数,常用通话时段,用户称谓词,用户关键词,用户关系，关系强度\n`;
            //增加\t为了不让表格显示科学计数法或者其他格式
            for(let i in jsonData ){
                    let str1 = `${jsonData[i].phoneNum1 + '\t'},`;
                    let str2 = `${jsonData[i].phoneNum2 + '\t'},`;
                    let str3 = `${jsonData[i].callNumber + '\t'},`;
                    let str4 = `${jsonData[i].messageNumber + '\t'},`;
                    let str5 = `${jsonData[i].contactTime + '\t'},`;
                    str5=str5.replace(/,/, "、")
                    let str6 = `${jsonData[i].appellation + '\t'},`;
                    let str7 = `${jsonData[i].topicKeyword + '\t'},`;
                    str7=str7.replace(/,/, "、")
                    let str8 = `${jsonData[i].relation + '\t'},`;
                    let str9 = `${jsonData[i].reStrength+ '\t'},`;
                str +=(str1+str2+str3+str4+str5+str6+str7+str8+str9)+'\n';
            }
            //encodeURIComponent解决中文乱码
            let uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(str);
            //通过创建a标签实现
            let link = document.createElement("a");
            link.href = uri;
            //对下载的文件命名
            link.download =  "用户关系表.xls";
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
        }
